package dk.aau.walkietalkie.picture;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import dk.aau.walkietalkieui.ChatActivity;
import dk.aau.walkietalkieui.R;

public class SendPicture extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popup_image);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String imgPath = extras.getString(ChatActivity.IMAGE_DATA);
			ImageView img = (ImageView) findViewById(R.id.ivShow);

			BitmapFactory.Options options = new BitmapFactory.Options();

			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(imgPath, options);

			int originalWidth = options.outWidth;
			int originalHeight = options.outHeight; // won't use this now

			Display display = getWindowManager().getDefaultDisplay();
			int scale = 1;			
			int imageViewWidth = display.getWidth();
			int imageViewHeight = display.getHeight();

			while(originalWidth/scale >= imageViewWidth && originalHeight/scale >= imageViewHeight){
				scale *= 2;
			}
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			
			
			// img.getMeasuredWidth();

//			int scaleFactor = Math
//					.round(((float) originalWidth / (float) imageViewWidth));
//
//			int newWidth = Math.round((float) originalWidth / scaleFactor);
//			int newHeight = Math.round((float) originalHeight / scaleFactor);

			// options.inSampleSize = scaleFactor;

			// options.inPreferredConfig = Bitmap.Config.RGB_565;

			Bitmap newBitmap = BitmapFactory.decodeFile(imgPath,o2);

			// get downscaled bitmap
//			newBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight,
//					true);
			img.setImageBitmap(newBitmap);
			img.invalidate();

			// img.setImageBitmap(BitmapFactory.decodeFile(imgPath));
		}
		Button btnImageCancel = (Button) findViewById(R.id.btnImageCancel);
		btnImageCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Don't send the picture
				// getIntent().putExtra("Send_picture", false);
				// setResult(ChatActivity.REQUEST_SEND_IMAGE, getIntent());
				finish();
			}
		});
	}
}
